package com.neusoft.elm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.elm.po.Food;
import com.neusoft.elm.service.FoodService;
import com.neusoft.elm.service.impl.FoodServiceImpl;

public class FoodController {
    
    /**
     * 根据商家ID查询商品列表
     */
    public Object listFoodByBusinessId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        FoodService service = new FoodServiceImpl();
        List<Food> list = service.listFoodByBusinessId(businessId);
        return list;
    }
    
    /**
     * 新增商品
     */
    public Object saveFood(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String foodName = request.getParameter("foodName");
        String foodExplain = request.getParameter("foodExplain");
        String foodImg = request.getParameter("foodImg");
        String foodPriceStr = request.getParameter("foodPrice");
        String businessIdStr = request.getParameter("businessId");
        String remarks = request.getParameter("remarks");
        
        Map<String, Object> result = new HashMap<>();
        
        // 参数验证
        if (foodName == null || foodName.trim().isEmpty()) {
            result.put("code", -1);
            result.put("error", "商品名称不能为空");
            return result;
        }
        
        if (foodPriceStr == null || foodPriceStr.trim().isEmpty()) {
            result.put("code", -1);
            result.put("error", "商品价格不能为空");
            return result;
        }
        
        if (businessIdStr == null || businessIdStr.trim().isEmpty()) {
            result.put("code", -1);
            result.put("error", "商家ID不能为空");
            return result;
        }
        
        try {
            Double foodPrice = Double.valueOf(foodPriceStr);
            Integer businessId = Integer.valueOf(businessIdStr);
            
            if (foodPrice <= 0) {
                result.put("code", -1);
                result.put("error", "商品价格必须大于0");
                return result;
            }
            
            Food food = new Food();
            food.setFoodName(foodName.trim());
            food.setFoodExplain(foodExplain != null ? foodExplain.trim() : "");
            food.setFoodImg(foodImg != null ? foodImg.trim() : "");
            food.setFoodPrice(foodPrice);
            food.setBusinessId(businessId);
            food.setRemarks(remarks != null ? remarks.trim() : "");
            
            FoodService service = new FoodServiceImpl();
            int saveResult = service.saveFood(food);
            
            if (saveResult > 0) {
                result.put("code", 1);
                result.put("message", "商品添加成功");
            } else {
                result.put("code", -1);
                result.put("error", "商品添加失败");
            }
            
        } catch (NumberFormatException e) {
            result.put("code", -1);
            result.put("error", "价格或商家ID格式错误");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", -1);
            result.put("error", "系统错误，请稍后重试");
        }
        
        return result;
    }
    
    /**
     * 修改商品
     */
    public Object updateFood(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String foodIdStr = request.getParameter("foodId");
        String foodName = request.getParameter("foodName");
        String foodExplain = request.getParameter("foodExplain");
        String foodImg = request.getParameter("foodImg");
        String foodPriceStr = request.getParameter("foodPrice");
        String remarks = request.getParameter("remarks");
        
        Map<String, Object> result = new HashMap<>();
        
        // 参数验证
        if (foodIdStr == null || foodIdStr.trim().isEmpty()) {
            result.put("code", -1);
            result.put("error", "商品ID不能为空");
            return result;
        }
        
        if (foodName == null || foodName.trim().isEmpty()) {
            result.put("code", -1);
            result.put("error", "商品名称不能为空");
            return result;
        }
        
        if (foodPriceStr == null || foodPriceStr.trim().isEmpty()) {
            result.put("code", -1);
            result.put("error", "商品价格不能为空");
            return result;
        }
        
        try {
            Integer foodId = Integer.valueOf(foodIdStr);
            Double foodPrice = Double.valueOf(foodPriceStr);
            
            if (foodPrice <= 0) {
                result.put("code", -1);
                result.put("error", "商品价格必须大于0");
                return result;
            }
            
            Food food = new Food();
            food.setFoodId(foodId);
            food.setFoodName(foodName.trim());
            food.setFoodExplain(foodExplain != null ? foodExplain.trim() : "");
            food.setFoodImg(foodImg != null ? foodImg.trim() : "");
            food.setFoodPrice(foodPrice);
            food.setRemarks(remarks != null ? remarks.trim() : "");
            
            FoodService service = new FoodServiceImpl();
            int updateResult = service.updateFood(food);
            
            if (updateResult > 0) {
                result.put("code", 1);
                result.put("message", "商品修改成功");
            } else {
                result.put("code", -1);
                result.put("error", "商品修改失败");
            }
            
        } catch (NumberFormatException e) {
            result.put("code", -1);
            result.put("error", "商品ID或价格格式错误");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", -1);
            result.put("error", "系统错误，请稍后重试");
        }
        
        return result;
    }
    
    /**
     * 删除商品
     */
    public Object deleteFoodById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String foodIdStr = request.getParameter("foodId");
        
        Map<String, Object> result = new HashMap<>();
        
        if (foodIdStr == null || foodIdStr.trim().isEmpty()) {
            result.put("code", -1);
            result.put("error", "商品ID不能为空");
            return result;
        }
        
        try {
            Integer foodId = Integer.valueOf(foodIdStr);
            
            FoodService service = new FoodServiceImpl();
            int deleteResult = service.deleteFoodById(foodId);
            
            if (deleteResult > 0) {
                result.put("code", 1);
                result.put("message", "商品删除成功");
            } else {
                result.put("code", -1);
                result.put("error", "商品删除失败");
            }
            
        } catch (NumberFormatException e) {
            result.put("code", -1);
            result.put("error", "商品ID格式错误");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", -1);
            result.put("error", "系统错误，请稍后重试");
        }
        
        return result;
    }
    
    /**
     * 根据ID查询商品
     */
    public Object getFoodById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String foodIdStr = request.getParameter("foodId");
        
        if (foodIdStr == null || foodIdStr.trim().isEmpty()) {
            return null;
        }
        
        try {
            Integer foodId = Integer.valueOf(foodIdStr);
            FoodService service = new FoodServiceImpl();
            Food food = service.getFoodById(foodId);
            return food;
        } catch (NumberFormatException e) {
            System.out.println("商品ID格式错误: " + foodIdStr);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}